/**
 * All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
 * <p>
 * PmRecBillService
 * 项目收款
 * <p>
 * 修改纪录
 * 2020-05-20 版本：1.0 wushenghong 创建。
 *
 * @version 版本：1.0
 * @author 作者：wushenghong
 * 创建日期：2020-05-20
 */
package com.topscomm.pm.service.impl;

import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.util.PrimaryKeyUtil;
import com.topscomm.pm.pojo.PmRecBillEntity;
import com.topscomm.pm.service.IPmProjectService;
import com.topscomm.pm.service.IPmRecBillService;
import com.topscomm.pm.service.impl.auto.PmRecBillServiceAuto;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import org.springframework.transaction.annotation.Transactional;

@Service(value = "pmRecBillService")
@Scope(value = "singleton")
public class PmRecBillService extends PmRecBillServiceAuto implements IPmRecBillService {
    @Resource(name = "pmProjectService")
    private IPmProjectService pmProjectService;

    /**
     * @param entity
     * @throws
     * @author: wushenghong
     * @date: 2020-05-20
     * @description:单号生成
     * @modify:
     */
    @Override
    protected void beforeInsert(BasicEntity entity) {
        super.beforeInsert(entity);
        if (entity.getId() <= 0) {// 赋值Id和单号
            long[] arrayId = PrimaryKeyUtil.GeneIDs(entity.getTableName(), 1);
            entity.setId(arrayId[0]);
            String docNo = String.valueOf(arrayId[0]);
            docNo = docNo.substring(0, 6) + docNo.substring(9);
            entity.setValue(PmRecBillEntity.FieldDocno, docNo);
        }
    }

    // 插入收款信息后，计算项目的回款金额至项目基本信息表
    @Override
    protected void afterInsert(BasicEntity entity) {
        super.afterInsert(entity);

        this.updateRecbillMoneyOfProjectBaseAndReceiptBase(ConvertUtil.convertToString(entity.getId()));
    }

    /**
     * @param
     * @throws
     * @author: wushenghong
     * @date: 2020-06-13
     * @description:收款金额计算
     * @modify:
     */
    public void updateRecbillMoneyOfProjectBaseAndReceiptBase(String sourceId) {
        PmRecBillEntity pmRecBillEntity = this.queryById(sourceId);
        StringBufferProxy sql = new StringBufferProxy();
        //判断当前该单据是从ERP同步过来的单据(股份、电力)，  还是人为手动添加的(科远)。
        //  1、从ERP同步过来的单据，state=9，要把刚新增的当前单据的金额直接累加到项目的收款金额上
        //  2、人为手动添加的(科远)，因为是刚添加的，state不是9，需要走审批流，所以不能在新增的时候添加而是在审批通过后添加
        if(pmRecBillEntity.getState()==9){
           //当前单据执行到此方法时，state还不等于9
           sql.appendSingle("select sum(NVL(money,0)) as totalmoney from PmRecBill where projectid = {0} and state = 9 and id!='{1}' ", pmRecBillEntity.getProjectid(),sourceId);

           BigDecimal totalMoney = BigDecimal.ZERO;
           List<Map<String, Object>> moneyList = this.selectExecute(sql.toString());
           if ( moneyList.size()> 0) {
             BigDecimal partMoney = (BigDecimal) moneyList.get(0).get("totalmoney");
             totalMoney = totalMoney.add(partMoney);
           }

           //获取当前单据的收款金额
           BigDecimal nowMoney = pmRecBillEntity.getMoney();
           totalMoney = totalMoney.add(nowMoney);
           // =========================更新项目表中的回款金额(取消审批或者审批通过都重新计算一遍)=========================
           sql.clear();
           sql.appendSingle("update pmproject set recbillmoney = {0} where id = {1}", totalMoney, pmRecBillEntity.getProjectid());
           this.updateExecute(sql.toString());
           // =========================更新项目收据表中的回款金额(取消审批或者审批通过都重新计算一遍)=========================
        }
    }


    /**
     * @author: wudaliang
     * @date: 2021-11-08
     * @param: sourceId
     * @description: 项目收款审批通过后，执行该方法
     * @return: void
     * @modify:
     */
    @Override
    @Transactional
    public void updateRecBillMoneyToProject(String sourceId) {
        PmRecBillEntity pmRecBillEntity = this.queryById(sourceId);
        StringBufferProxy sql = new StringBufferProxy();

        //当前单据执行到此方法时，state还不等于9
        sql.appendSingle("select sum(NVL(money,0)) as totalmoney from PmRecBill where projectid = {0} and state = 9 and id!='{1}' ", pmRecBillEntity.getProjectid(),sourceId);

        BigDecimal totalMoney = BigDecimal.ZERO;
        List<Map<String, Object>> moneyList = this.selectExecute(sql.toString());
        if ( moneyList.size()> 0) {
            BigDecimal partMoney = (BigDecimal) moneyList.get(0).get("totalmoney");
            totalMoney = totalMoney.add(partMoney);
        }

        BigDecimal nowMoney = pmRecBillEntity.getMoney();
        totalMoney = totalMoney.add(nowMoney);

        sql.clear();
        sql.appendSingle("update pmproject set recbillmoney = {0} where id = {1}", totalMoney, pmRecBillEntity.getProjectid());
        this.updateExecute(sql.toString());
    }
}
